# credit : http://code.activestate.com/recipes/146306/

import httplib, mimetypes

def post_multipart(host, selector, fields, files):
	"""
	Post fields and files to an http host as multipart/form-data.
	fields is a sequence of (name, value) elements for regular form fields.
	files is a sequence of (name, filename, value) elements for data to be uploaded as files
	Return the server's response page.
	"""
	content_type, body = encode_multipart_formdata(fields, files)
	h = httplib.HTTP(host)
	h.putrequest('POST', selector)
	h.putheader('content-type', content_type)
	h.putheader('content-length', str(len(body)))
	h.endheaders()
	h.send(body)
	errcode, errmsg, headers = h.getreply()
	return h.file.read()

def encode_multipart_formdata(fields, files):
	"""
	fields is a sequence of (name, value) elements for regular form fields.
	files is a sequence of (name, filename, value) elements for data to be uploaded as files
	Return (content_type, body) ready for httplib.HTTP instance
	"""
	BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
	CRLF = '\r\n'
	L = []
	for (key, value) in fields:
		L.append('--' + BOUNDARY)
		L.append('Content-Disposition: form-data; name="%s"' % key)
		L.append('')
		L.append(value)
	for (key, filename, value) in files:
		L.append('--' + BOUNDARY)
		L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
		L.append('Content-Type: %s' % get_content_type(filename))
		L.append('')
		L.append(value)
	L.append('--' + BOUNDARY + '--')
	L.append('')
	body = CRLF.join(L)
	content_type = 'multipart/form-data; boundary=%s' % BOUNDARY

	#print content_type
	#print body

	return content_type, body

def get_content_type(filename):
	return mimetypes.guess_type(filename)[0] or 'application/octet-stream'


##########################################
##########################################
import urlparse

def posturl(url, fields, files):
    urlparts = urlparse.urlsplit(url)
    return post_multipart(urlparts[1], urlparts[2], fields,files)

'''
- media (required) - Binary image data
- username (required) - Twitter username
- password (required) - Twitter password
- message (optional) - Message to post to twitter. The URL of the image is automatically added.
'''


class TwitPicApi :

	def __init__(self, username, password):
		self.twitt_user = username
		self.twitt_pass = password
		pass

	
	def post(self, message, image):
		url = "http://twitpic.com/api/uploadAndPost"
		fields = []
		username = ("username", self.twitt_user)
		password = ("password", self.twitt_pass)
		message = ("message", message)

		fields.append(username)
		fields.append(password)
		fields.append(message)
	
		ib = open(image,"rb").read()

		files = [("media", image , ib)]
		result = posturl(url, fields, files)
		print result

		

if __name__ == "__main__" :
	url = "http://twitpic.com/api/uploadAndPost"
	fields = []
	username = ("username", "wearetherock")
	password = ("password", "=v[ohv'9hk")
	message = ("message", "test")

	fields.append(username)
	fields.append(password)
	fields.append(message)
	
	d = open("snapshot/test.png","rb").read()

	files = [("media", "snapshot/test.png", d)]
	

	result = posturl(url, fields, files)
	print result
	



	
